﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HttpClientStudy.UnitTest.HttpClients
{
    public class PipelineClientTest
    {
        private readonly ITestOutputHelper _logger;

        public PipelineClientTest(ITestOutputHelper outputHelper)
        {
            _logger = outputHelper;
        }

        /// <summary>
        /// 手动处理
        /// </summary>
        [Fact]
        public async Task Delegating_Test()
        {
            //构建管道
            var handler = new CustomHeadersHandler()
            {
                //相当于下一个中间件(管道)
                InnerHandler = new SocketsHttpHandler() 
                {
                    AllowAutoRedirect = true
                }
            };

            //构造中传入管道对象
            HttpClient httpClient = new HttpClient(handler);

            var sd = await httpClient.GetAsync(WebApiConfigManager.GetWebApiConfig().BaseUrl + "/api/health");
            var contentText = await sd.Content.ReadAsStringAsync();

            _logger.WriteLine(contentText);
        }

        [Fact]
        public async Task CreateHttpClient_Test()
        {
            HttpClient client = new PipelineHttpClient().CreateHttpClient();

            var r = await client.GetAsync(WebApiConfigManager.GetWebApiConfig().BaseUrl + "/api/health");
            r.EnsureSuccessStatusCode();
        }
    }
}
